<%--
  ~ Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  ~ Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
  ~ Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
  ~ Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
  ~ Vestibulum commodo. Ut rhoncus gravida arcu.
  --%>

<%--
  Created by IntelliJ IDEA.
  User: dengdc
  Date: 2017/7/19
  Time: 17:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>编辑</title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">
        var isReadOnly = '1' === '${readonly}';
        var editViewLayerId = 0;
        var formSetting = {
            title: "协议内容",
            module: 'save',
            columns: [
                {
                    name: "id",
                    hidden: true
                }, {
                    name: "name",
                    label: "名称",
                    readonly: isReadOnly
                }, {
                    name: "supplierId",
                    label: "业务供应商",
                    type: "dropdown",
                    dropdownType: "ajax",
                    url: "<s:url value='/order-dropdown/supCompany'/>",
                    op: "eq",
                    minLength: 0,
                    keyWordName: "key",
                    success: function (data) {
                        data = data.results;
                        var array = new Array();
                        $.each(data, function (i, n) {
                            array.push([n["name"], n["value"]]);
                        });
                        return array;
                    },
                    validate: 'validate[required]',
                    readonly: isReadOnly
                }, {type: "spaceHolder"}, {
                    name: "eftimeStart",
                    label: "生效时间",
                    type: 'date',
                    readonly: isReadOnly
                }, {
                    name: "eftimeEnd",
                    label: "失效时间",
                    type: 'date',
                    readonly: isReadOnly
                }, {
                    name: "remark",
                    label: "备注",
                    type: 'textarea',
                    colspan: 3,
                    readonly: isReadOnly
                }, {
                    name: "file",
                    realName: "attachment",
                    label: "附件",
                    type: 'file',
                    readonly: isReadOnly,
                    href: 'file.id',
                    colspan: 2
                }],
            form: {
                'url': 'saveOrUpdate',
                'data': function () {
                    var dataValue = "";
                    $.ajax({
                               url: "editData?id=${param.id}&feeIeType=${param.feeIeType}",
                               async: false,
                               success: function (data) {
                                   dataValue = data;
                               }
                           });
                    return dataValue;
                },
                'ajaxForm': {
                    success: function (result) {
                        layer.msg(result.msg);
                    }
                }
            },
            buttons: {
                custom: [{
                    id: "addProtocol",
                    value: "添加明细",
                    index: 1,
                    click: function () {
                        editViewLayerId = layer.open({
                                                         type: 2,
                                                         title: false,
                                                         content: '<s:url value="/fund/protocol/manage/editView"/>?uniteId=${param.id}',
                                                         area: ['840px', '400px'],
                                                         success: function (layero, index) {
                                                             var $layer0 = $(layero[0]);
                                                             var layerIframeBodyHeight = $($layer0.find(
                                                                 'iframe')[0].contentDocument.body).height();
                                                             $layer0.height(layerIframeBodyHeight + 60, 2000);
                                                         }
                                                     });
                    }
                }, {
                    id: "editProtocol",
                    value: "编辑明细",
                    index: 2,
                    click: function () {
                        var selId = $("#protocolGrid").gridMethods().getId();
                        if (!selId) {
                            layer.msg("请先选择一行");
                            return;
                        }
                        editViewLayerId = layer.open({
                                                         type: 2,
                                                         title: false,
                                                         content: '<s:url value="/fund/protocol/manage/editView?uniteId=${param.id}&id="/>' + selId,
                                                         area: ['840px', '320px'],
                                                         success: function (layero, index) {
                                                             var $layer0 = $(layero[0]);
                                                             $($layer0.find(
                                                                 'iframe')[0].contentDocument.body).height();
                                                         }
                                                     });
                    }
                }, {
                    id: "returnMangeView",
                    value: "返回",
                    index: 1,
                    click: function () {
                        location.href = '<s:url value="/fund/protocol/manage/unite/view"/>'
                    }
                }, {
                    id: "reloadGrid",
                    value: "刷新表格",
                    index: 4,
                    click: function () {
                        $("#protocolGrid").gridMethods().reload();
                    }
                }]
            }
        };
        var gridSetting = {
            grid: {
                id: "protocolGrid",
                title:"协议明细",
                url: "<s:url value="/fund/protocol/manage/viewData"/>?uniteId=${param.id}",
                colModel: [{
                    index: "id",
                    name: "id",
                    hidden: true
                }, {
                    name: "name",
                    label: "费用名称",
                    align: 'center',
                    width: 150
                }, {
                    name: "feeType",
                    label: "费用类型",
                    align: 'center',
                    width: 150
                }, {
                    name: "bizType",
                    label: "业务类型",
                    align: 'center',
                    width: 150
                }, {
                    name: "ieType",
                    label: "进出口",
                    align: 'center',
                    width: 150
                }, {
                    name: "countRule",
                    label: "计费规则",
                    align: 'center',
                    width: 150
                }, {
                    name: "startCount",
                    label: "起算数量",
                    align: 'center',
                    width: 150
                }, {
                    name: "unitPriceIn",
                    label: "报价",
                    align: 'center',
                    width: 80
                }, {
                    name: "unitPriceOut",
                    label: "成本",
                    align: 'center',
                    width: 80
                }, {
                    name: "capPriceIn",
                    label: "报价",
                    align: 'center',
                    width: 80
                }, {
                    name: "capPriceOut",
                    label: "成本",
                    align: 'center',
                    width: 80
                }, {
                    name: "descript",
                    label: "描述",
                    width: 150
                }],
                gridComplete: function () {
                    var firstTr = $(".jqgfirstrow");
                    var tds = firstTr.find("td:visible");
                    var secondTr = $(".jqgrow:first");
                    var stds = secondTr.find("td:visible");

                    for (var i = 0; i < tds.length; i++) {
                        var td =  $(tds[i]);
                        var std = $(stds[i]);
                        var th = $("#" + std.attr("aria-describedby"));
                        td.width(th.width());
                    }
                }
            }
        };
        $(document).ready(function () {
            $("#gridTable").mrdForm(gridSetting);
            jQuery("#protocolGrid").jqGrid('setGroupHeaders', {
                useColSpanStyle: true,
                groupHeaders: [
                    {startColumnName: 'unitPriceIn', numberOfColumns: 2, titleText: '<em>单价</em>'},
                    {startColumnName: 'capPriceIn', numberOfColumns: 2, titleText: '<em>封顶价</em>'}
                ]
            });
            $("#table").mrdForm(formSetting);
            moveBtn();
        });
        function moveBtn(){
            var btnTr = $("tr.mrdBtnTr");
            var btnContainer = btnTr.find("td#mf_buttons_container");
            btnContainer.attr("colspan", 3);
            btnContainer.css("text-align", "right");

            var btnContainerClone = btnContainer.clone();
            btnContainerClone.find("input:button").remove();
            btnContainerClone.attr({
                "id":"mf_buttons_container2",
                "colspan":"3"
                                   });
            btnContainerClone.appendTo(btnTr);

            $("#reloadGrid").appendTo(btnContainerClone);

            $("#addProtocol").appendTo(btnContainerClone);

            $("#editProtocol").appendTo(btnContainerClone);
        }

        function reloadGridData(){
            $("#protocolGrid").gridMethods().reload();
        }

        function closeEditView(){
            layer.close(editViewLayerId);
        }

        $.crftGridData = {showData:[], trueData:[]};

        function copyModel(){
            //todo step1 get copied model
            $.ajax({
                url:'/fund/protocol/manage/getCopy',
                method:'post',
                dataType:'json',
                data:{},
                success:function(data){

                }
                   });
            //todo step2 data initialization
            //todo step3 change #saveBtn click function
            //todo step4 change protocol save action
            //todo step5 do save all
        }
    </script>
    <style type="text/css">
        .ui-autocomplete {
            max-height: 198px;
            overflow-y: auto;
        }
    </style>
</head>
<body>
<div id="table"></div>
<div id="gridTable"></div>
</body>
</html>
